package com.yanyanr.yr.greendaosample.greendao;

import android.content.Context;
import com.yanyanr.yr.greendaosample.greendao.gen.DaoMaster;
import com.yanyanr.yr.greendaosample.greendao.gen.DaoSession;
import com.yanyanr.yr.greendaosample.greendao.gen.UserModelDao;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.query.QueryBuilder;

/**
 * Created by YR on 2016/7/21.
 */
public class DbHelper {

    private static Context mcontext ;

    /**
     * 单例
     */
    private static DbHelper helper;

    /**
     * 数据库辅助操控类
     */
    private static DaoMaster.OpenHelper mOpenHelper;

    /**
     * 数据库与Dao管理类
     */
    private  static DaoMaster mDaoMaster;

    /**
     * DAO会话管理
     */
    private static  DaoSession mDaoSession;

    /**
     * 数据库名称 ，不指定，就默认
     */
    public static String DATABASE_NAME = "spd.db";

    /**
     * 初始化上下文
     * @param context
     */
    public static  void init(Context context){
        mcontext = context;

    }

    /**
     * 设置数据库的名称
     */
    public static  void setDbName(String dbName){
        DATABASE_NAME = dbName;
    }

    /**
     * 单例模式
     * @return
     */
    public static synchronized DbHelper getInstance(){

        if(helper != null){
            if(mOpenHelper == null ){
                Instance();
            }
            return  helper;
        }else{
            helper  = new DbHelper();
            Instance();
        }

        return  helper;
    }

    /**
     * 创建数据库
     */
    static void Instance(){

//       mOpenHelper = new DaoMaster.DevOpenHelper(mcontext, DATABASE_NAME);//默认helper,删除所有表
         mOpenHelper = new mOpenHelp(mcontext, DATABASE_NAME);//自定义helper来
         mDaoMaster = new DaoMaster(mOpenHelper.getWritableDb());
         mDaoSession = mDaoMaster.newSession();

        //打印查询语句Log
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
    }

    /**
     * 自定义OpenHelp，重写onUpgrade，因为默认升级会删除掉所有表
     */
    public static class mOpenHelp extends DaoMaster.OpenHelper {

        public mOpenHelp(Context context, String name) {
            super(context, name);
        }

        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            super.onUpgrade(db, oldVersion, newVersion);

        }
    }

    /**
     * 获取UserModel表DAO类
     *
     * @return
     */
    public UserModelDao getUserModelDao() {
//        mDaoSession.getDao(UserModel.class);//也可以这样获取
        return mDaoSession.getUserModelDao();
    }
    /**
     * 清空数据库表的数据,保留索引.
     */
    public void deleteAllTables() {
        getUserModelDao().deleteAll();
    }

    /**
     * 获取当前数据库名称和版本，测试用
     * @return
     */
    public String getDbInfo(){
        return mOpenHelper.getDatabaseName() + ",version:"+mDaoMaster.getSchemaVersion();
    }

    /**
     * 关闭数据库,注销相关对象
     */
    public void shotdown(){

        if (mOpenHelper != null){
            mOpenHelper.close();
            mOpenHelper = null;
        }
        if (mDaoSession != null) {
            mDaoSession.clear();
            mDaoSession = null;
        }

        mDaoMaster = null;
    }
}
